#import "RNPreferencesController.h"
#import "RNConstants.h"

@interface RNPreferencesController (RNPreferencesControllerPrivateMethods)
-(void)selectView_:(NSView *)subview;
-(void)updateClearHistoryPopUpTitles_;
@end

@implementation RNPreferencesController

@synthesize generalView = generalView_;
@synthesize updateView = updateView_;
@synthesize toolbar = toolbar_;
@synthesize clearHistoryComponentPopUp = clearHistoryComponentPopUp_;
@synthesize clearHistoryTextField = clearHistoryTextField_;

-(void)awakeFromNib {
    [self updateClearHistoryPopUpTitles_];
}

- (void)controlTextDidChange:(NSNotification *)aNotification {
    [self updateClearHistoryPopUpTitles_];
}

- (BOOL)control:(NSControl *)control textShouldEndEditing:(NSText *)fieldEditor {
    NSInteger currentIntervalAmount = [[self clearHistoryTextField] integerValue];
    if (currentIntervalAmount <= 0) {
        [[self clearHistoryTextField] setIntegerValue:1];
        [self updateClearHistoryPopUpTitles_];
    }
    if (currentIntervalAmount > 100)
        [[self clearHistoryTextField] setIntegerValue:100];
    return YES;
}

-(void)updateClearHistoryPopUpTitles_ {
    NSString *intervalString = [[self clearHistoryTextField] stringValue];
    NSInteger currentIntervalAmount = [[self clearHistoryTextField] integerValue];
    if ([intervalString length] == 0 || (currentIntervalAmount == 0 && ![intervalString isEqualToString:@"0"]))
        return;
    BOOL displayAsMultiple = currentIntervalAmount != 1;
    NSMenu *clearHistoryPopUpMenu = [[self clearHistoryComponentPopUp] menu];
    NSArray *clearHistoryMenuItems = [clearHistoryPopUpMenu itemArray];
    for (NSMenuItem *item in clearHistoryMenuItems) {
        if ([item tag] == -1 || [item isSeparatorItem])
            continue;
        NSString *title = [item title];
        unichar lastCharacter = [title characterAtIndex:[title length] - 1];
        BOOL titleIsMultiple = lastCharacter == 's';
        if (displayAsMultiple == titleIsMultiple)
            continue;
        if (displayAsMultiple) 
            [item setTitle:[NSString stringWithFormat:@"%@s", title]];
        else
            [item setTitle:[title substringToIndex:[title length] - 1]];
    }
}

-(IBAction)selectGeneralView:(id)sender {
    [[self window] setTitle:@"General"];
    [self selectView_:[self generalView]];
}

-(IBAction)selectUpdateView:(id)sender {
    [[self window] setTitle:@"Update"];
    [self selectView_:[self updateView]];
}

-(void)showPreferences {
    [[self window] center];
    [self selectGeneralView:nil];
    [[self toolbar] setSelectedItemIdentifier:@"RNGeneralToolbarItem"];
    [self showWindow:nil];
}

-(void)selectView_:(NSView *)subview {
    NSView *contentView = [[self window] contentView];
    [[contentView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [contentView addSubview:subview];
    [subview setFrameOrigin:NSZeroPoint];
    NSRect windowFrame = [[self window] frame];
    NSRect subviewBounds = [subview bounds];
    NSRect newWindowFrame = [[self window] frameRectForContentRect:subviewBounds];
    windowFrame.origin.y = NSMaxY(windowFrame) - NSHeight(newWindowFrame);
    windowFrame.size = newWindowFrame.size;
    [[self window] setFrame:windowFrame display:YES animate:YES];
}

@end
